package wiki.kaizen.cloud.plugin.spring.scan.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import wiki.kaizen.cloud.plugin.spring.deserializer.LocalDateDeserializer;
import wiki.kaizen.cloud.plugin.spring.deserializer.LocalDateTimeDeserializer;
import wiki.kaizen.cloud.plugin.spring.deserializer.LocalTimeDeserializer;
import wiki.kaizen.cloud.plugin.spring.deserializer.StringDeserializer;
import wiki.kaizen.cloud.plugin.spring.serializer.LocalDateSerializer;
import wiki.kaizen.cloud.plugin.spring.serializer.LocalDateTimeSerializer;
import wiki.kaizen.cloud.plugin.spring.serializer.LocalTimeSerializer;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
/**
* 自定义消息转换器
* 使用 fastjson message convert 处理
* @author xeepoo
* */
@Configuration
public class MessageConvertConfig{

    @Bean
    public FastJsonHttpMessageConverter httpMessageConverter(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        // FastJsonConfig
        FastJsonConfig config = new FastJsonConfig();
        ParserConfig parserConfig = config.getParserConfig();
        /*
        *  设置全局反序列化 转化器
        * */
        parserConfig.putDeserializer(String.class,new StringDeserializer());
        parserConfig.putDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
        parserConfig.putDeserializer(LocalDate.class, new LocalDateDeserializer());
        parserConfig.putDeserializer(LocalTime.class, new LocalTimeDeserializer());
        ParserConfig.global = parserConfig;
        /*
        * 序列化
         * */
        SerializeConfig serializeConfig = config.getSerializeConfig();
        serializeConfig.put(LocalDateTime.class, new LocalDateTimeSerializer());
        serializeConfig.put(LocalDate.class, new LocalDateSerializer());
        serializeConfig.put(LocalTime.class, new LocalTimeSerializer());

        //serializeConfig.
        /*
        *重写 response的 contentlength
        * */
        config.setWriteContentLength(true);
        //日期格式化
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        config.setCharset(StandardCharsets.UTF_8);
        config.setSerializerFeatures(
            //关闭循环引用 相同json数据对象会被设置为 如 $ref: "$.data.records[0].departurePort"
            SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.PrettyFormat,
            SerializerFeature.WriteDateUseDateFormat //时间类型 条用 format 方法
        );

        // 设置 mediaTypes
        List<MediaType> mediaTypes = Arrays.asList(
                MediaType.APPLICATION_JSON,
                MediaType.TEXT_PLAIN,
                MediaType.APPLICATION_FORM_URLENCODED,
                MediaType.MULTIPART_FORM_DATA
        );
        converter.setSupportedMediaTypes(mediaTypes);

        //设置config
        converter.setFastJsonConfig(config);
        return converter;
    }
}
